//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	Smart.Common.EventReceiver.cs
//  Developer:	Solomatov Igor
//  Created:	03.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Smart.Common.Router;

namespace Smart.Common.Features
{
	/// <summary>
	/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
	/// </summary>
	/// <remarks>
	/// The GUID attached to this class may be used during packaging and should not be modified.
	/// </remarks>
	[Guid("eab2a1a0-46cd-450b-b3ac-2d9da0e94782")]
	public class SPCommonEventReceiver : SPFeatureReceiver
	{
		public override void FeatureActivated(SPFeatureReceiverProperties properties)
		{
			try
			{
				var web = (SPWeb)properties.Feature.Parent;

				using (SPH.SetContext(web.Site, web))
				{
					RegisterAddon();

					SetupRouter();

					web.BindFormTemplates(true, new SPContentTypeId(Const.Configuration.ContentTypeId), Const.Forms.WebForms.ListFormTemplate, Const.Forms.WebForms.ListFormTemplate, Const.Forms.WebForms.ListFormTemplate);
				}
			}
			catch (Exception x)
			{
				H.LX("SPCommonEventReceiver.FeatureActivated", x);
			}
		}

		public virtual void SetupRouter()
		{
			SPServices.RouterFactory.AddOrUpdateTemplate(
				Const.Router.Web.ServiceName,
				new RouterTemplate
					{
						Name = Const.Router.LibTemp.RouterKey,
						Strict = false,
						Type = Const.Router.LibTemp.RouterType,
						Uri = Const.Router.LibTemp.RouterUri,
					}
				);
		}

		public virtual void RegisterAddon()
		{
			// Add addon
			SPServices.AddonManager.Reg(typeof(AddonCommon).AssemblyQualifiedName);

			// Register and Load addons
			SPServices.AddonManager.Reload();
		}
	}
}